package com.gridmobi.xrtb.saveclick;

import com.google.common.collect.Maps;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Map;

public class CounterClickHandler {
    /*OfferID,Hour,Subid*/
    public static Map<Integer, Map<Integer, Map<String, CntClick>>> CURRENT_CLICKS = Maps.newConcurrentMap();

    public static void countClick(Integer oid, String subid, boolean iserr) {
        int hour =  LocalDateTime.now(ZoneOffset.UTC).getHour();
        if (!CURRENT_CLICKS.containsKey(oid)) {
            CURRENT_CLICKS.put(oid, Maps.newConcurrentMap());
        }
        if (!CURRENT_CLICKS.get(oid).containsKey(hour)) {
            CURRENT_CLICKS.get(oid).put(hour, Maps.newConcurrentMap());
        }
        if (!CURRENT_CLICKS.get(oid).get(hour).containsKey(subid)) {
            CURRENT_CLICKS.get(oid).get(hour).put(subid, new CntClick());

        }
        if (iserr) {
            CURRENT_CLICKS.get(oid).get(hour).get(subid).getErrclicks().incrementAndGet();

        } else {
            CURRENT_CLICKS.get(oid).get(hour).get(subid).getClicks().incrementAndGet();

        }
    }

}
